import { Controller, Post, Query } from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
import { TasksService } from './tasks.service';

@ApiTags('tasks')
@Controller('tasks')
export class TasksController {
  constructor(private readonly tasksService: TasksService) {}

  @Post('trigger-feishu-task')
  @ApiOperation({ summary: '手动触发飞书数据获取和微信发送任务' })
  @ApiQuery({
    name: 'isPublish',
    required: false,
    type: Boolean,
    description:
      '是否获取 publish 为 1 的数据，默认为 false，获取 publish 为 0 的数据',
  })
  async triggerFeishuTask(@Query('isPublish') isPublish?: string) {
    // 将字符串转换为布尔值
    const isPublishBool = isPublish === 'true' || isPublish === '1';
    await this.tasksService.handleFeishuDataCron(isPublishBool);
    return {
      success: true,
      message: `已触发飞书数据获取和微信发送任务，获取 publish 为 ${isPublishBool ? '1' : '0'} 的数据`,
    };
  }

  @Post('update-cache')
  @ApiOperation({ summary: '手动触发更新缓存数据任务' })
  async updateCache() {
    await this.tasksService.updateCacheDataCron();
    return {
      success: true,
      message: '已触发更新缓存数据任务',
    };
  }
}
